{template 'common'}
<div id="wallMain">
	<div id="topbox" class="topbox">
		<div class="topbox_l">
			<div class="topic">
				<h1 class="msg_tit">已中奖用户：<strong class="red">0</strong> 位，</h1>
				<span class="addCnt">
				<a href="{php echo $this->createWebUrl('winner', array('id'=>$rid));}" target="_blank" style="color:#FFF; text-decoration:underline;">点击查看中奖名单</a>
				</span>
			</div>
		</div>
	</div>
	<div >
		<div class="win_animate"></div>
		<div class="win_list"><table cellspacing="0" cellpadding="0"></table></div>
	</div>
</div>

<div class="side_div">
	<!--div class="side_item"><a href="{php echo $this->createWebUrl('qrcode', array('id' => $wall['rid']))}">二维码</a></div-->
	{if $_W['uid']}
	<div class="side_item"><a href="javascript:;" id="lottery">抽奖</a></div>
	<div class="side_item"><a href="{php echo $this->createWebUrl('winner', array('id' => $rid))}">返回</a></div>
	{/if}
</div>
<script type="text/javascript">
var message = {php echo json_encode($list)};
var wxwall = {
	'start' : true,
	'temp' : '',
	'intervalLottery' : {},
	'winUserNum' : 0,
	'messageNum' : 0,
	'winUser' : new Array(),
	'init' : function() {
		var $this = this;
		this.control('start');
	},
	'buildLottery' : function(message) {/*构造一条抽奖内容*/
		if (!message) {
			return false;
		}
		this.temp = message['avatar'];
		if (message['avatar']) {
			if(message['avatar'].indexOf('http') < 0){
				//message['avatar'] = '{$_W['attachurl']}' + message['avatar'];
				message['avatar'] = message['avatar'];
			}
		} else {
			message['avatar'] = '{$_W['attachurl']}' + 'images/global/noavatar_middle.gif';
		}
		var font_size = this.changeSize(message['content']);
		var html = '<div class="userPic" id="msg_'+message['id']+'"><img src="'+message['avatar']+'"><span class="userName"><strong>'+message['nickname']+'</strong></span></div><div class="msgBox"><span class="msgCnt" style="font-size:' + font_size + 'px;line-height:' + parseInt(font_size+8) + 'px;">' + message['content'] + '</span></div>';
		message['avatar'] = this.temp;
		return html;
	},
	'control' : function(operation) {
		var $this = this;
		if (operation == 'pause') {
			this.start = false;
			$('#lottery').html('抽奖');
			$('#lottery')[0].onclick = function() {
				$this.control('start');
			}
		} else if(operation == 'start') {
			this.start = true;
			$('#lottery').html('暂停');
			$('#lottery')[0].onclick = function() {
				$this.control('pause');
				$this.getuser();
			}
			$this.startLottery();
		}
	},
	'startLottery' :function() {
		var $this = this;
		if (!$this.start) {
			clearTimeout($this.intervalLottery);
			return false;
		}
		$('#lottery').html('暂停');
		do {
			if ($this.winUser.length >= message.length ) {
				alert('全部用户已中奖！');
				return false;
			}
			$this.messageNum++;
			if ($this.messageNum >= message.length) {
				$this.messageNum = 0;
			}
		}
		while (!message[$this.messageNum] || $this.isWinuser(message[$this.messageNum]['id']));

		$('.win_animate').html(this.buildLottery(message[$this.messageNum]));
		$this.intervalLottery = setTimeout(function(){
			$this.startLottery();
		}, 50);

	},
	'getuser' : function() {
		var $this = this;
		var winner = message[$this.messageNum];
		if($this.isWinuser(winner['id'])) {
			alert('抱歉，未有用户中奖，你重新抽奖！');
			$this.control('start');
			return false;
		}
		if ($this.winUser.length >= message.length ) {
			alert('全部用户已中奖！');
			return false;
		}
		this.winUserNum++;
		this.temp = winner['avatar'];
		if (winner['avatar']) {
			//avatar = '{$_W['attachurl']}' + winner['avatar'];
			avatar = winner['avatar'];
		} else {
			avatar = '{$_W['attachurl']}' + 'images/global/noavatar_middle.gif';
		}
		
		$('.win_list table').prepend('<tr><td class="num"><span>'+this.winUserNum+'</span></td><td class="avatar"><img src="'+avatar+'"></td><td class="name">'+winner['nickname']+'</td></tr>');
		$('.msg_tit strong').html(this.winUserNum);
		this.winUser.push(winner['id']);
		record();
		function record() {
			$.getJSON('{php echo $this->createWebUrl('addwinner', array('id' => $rid))}', {'mid' : winner['id']}, function(s){
				if (s.type == 'error') {
					record();
				}
			});
		}
	},
	'isWinuser' : function(username) {
		for(var i=0; i<this.winUser.length; i++){
			if(this.winUser[i] == username) {
				return true;
			}
		}
		return false;
	},
	'removeHTMLTag' : function(str) {
		str = str.replace(/<\/?[^>]*>/g,'');
		str = str.replace(/[ | ]*\n/g,'\n');
		str = str.replace(/\n[\s| | ]*\r/g,'\n');
		str = str.replace(/&nbsp;/ig,'');
		return str;
	},
	'strlen' : function(str) {
		var n = 0;
		str = this.removeHTMLTag(str);
		for(i=0;i<str.length;i++){
			var leg=str.charCodeAt(i);
			n+=1;
		}
		return n;
	},
	'changeSize' : function(a) {
		var $this = this;
		var str_len = parseInt($this.strlen(a));
		var font_size = 36;
		for (j=18;j>str_len;j--) {
			font_size += 4;
		}
		return font_size;
	}
};
$(function(){
	wxwall.init();
	var windowUrl = '{php echo $this->createWebUrl('lottery', array('id' => $id))}';
	$('.Topic_cnt').change(function() {
		window.location.href = windowUrl+'&type='+$(this).val();
	});
});
</script>
</body>
</html>
